/*
 *
 * Copyright (c) 2023 - future
 * @author fzh
 * @email e_341877120_mail@163.com
 * @link https://mvnrepository.com/search?q=io.github.xrfzh.cn
 *
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package cn.xrfzh.system.controller;

import cn.xrfzh.common.annotation.SystemLog;
import cn.xrfzh.common.enums.LogType;
import cn.xrfzh.system.common.base.MyPage;
import cn.xrfzh.system.common.base.Result;
import cn.xrfzh.system.common.groups.Save;
import cn.xrfzh.system.common.groups.Update;
import cn.xrfzh.system.entity.dictionary.Dictionary;
import cn.xrfzh.system.service.DictionaryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 字典表 (Dictionary)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/dictionary")
public class DictionaryController {

	/**
	 * 服务对象
	 */
	@Resource
	private DictionaryService dictionaryService;

	/**
	 * 通过父字典code 查询子集
	 * @param code 字典code
	 * @return
	 */
	@GetMapping("/listDicByParentCode")
	public Result<List<Dictionary>> listDicByParentCode(@NotBlank(message = "字典code不能为空") String code) {
		return Result.success(dictionaryService.listDicByParentCode(code));
	}

	/**
	 * 查询所有数据
	 * @param dictionary 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryAll")
	public Result<List<Dictionary>> queryAll(Dictionary dictionary) {
		return Result.success(dictionaryService.list(new QueryWrapper<>(dictionary)));
	}

	/**
	 * 分页查询所有数据
	 * @param page 分页对象
	 * @param dictionary 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryByPage")
	public Result<MyPage<Dictionary>> queryByPage(MyPage<Dictionary> page, Dictionary dictionary) {
		return Result.success(dictionaryService.queryByPage(page, dictionary));
	}

	/**
	 * 通过主键查询单条数据
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("/queryOne/{id}")
	public Result<Dictionary> selectOne(@PathVariable Serializable id) {
		return Result.success(dictionaryService.getById(id));
	}

	/**
	 * 新增字典
	 * @param dictionary 实体对象
	 * @return 新增结果
	 */
	@SystemLog(module = "字典模块", type = LogType.INSERT, description = "新增字典")
	@PostMapping("/insert")
	public Result<Object> insert(@Validated(Save.class) @RequestBody Dictionary dictionary) {
		return Result.isSuccess(dictionaryService.save(dictionary));
	}

	/**
	 * 更新字典
	 * @param dictionary 实体对象
	 * @return 修改结果
	 */
	@SystemLog(module = "字典模块", type = LogType.UPDATE, description = "更新字典")
	@PutMapping("/update")
	public Result<Object> update(@Validated(Update.class) @RequestBody Dictionary dictionary) {
		return Result.isSuccess(dictionaryService.updateById(dictionary));
	}

	/**
	 * 删除字典
	 * @param id 主键
	 * @return 删除结果
	 */
	@SystemLog(module = "字典模块", type = LogType.DELETE, description = "删除字典")
	@DeleteMapping("/delete/{id}")
	public Result<Object> delete(@PathVariable Long id) {
		Dictionary dictionary = new Dictionary();
		dictionary.setId(id);
		return Result.isSuccess(dictionaryService.removeById(dictionary));
	}

	/**
	 * 批量删除字典
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@SystemLog(module = "字典模块", type = LogType.DELETE, description = "批量删除字典")
	@DeleteMapping("/batchDelete")
	public Result<Object> batchDelete(
			@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		return Result.isSuccess(dictionaryService.batchDelete(idList, new Dictionary()));
	}

}
